package com.msb.mall.order.web;

import com.alibaba.fastjson.JSON;
import com.msb.common.constant.OrderConstant;
import com.msb.common.utils.R;
import com.msb.mall.order.config.AliPayTemplate;
import com.msb.mall.order.entity.OrderEntity;
import com.msb.mall.order.service.OrderService;
import com.msb.mall.order.vo.OrderBalanceVo;
import com.msb.mall.order.vo.OrderSubmitVo;
import com.msb.mall.order.vo.PayVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.ExecutionException;

@Controller
public class OrderWebController {

    @Autowired
    private AliPayTemplate payTemplate;

    @Autowired
    private OrderService orderService;

    /**
     * 结算页----填写收获地址等信息
     * @param model
     * @return
     */
    @RequestMapping("/toTrade")
    public String toTrade(Model model){
        try {
            OrderBalanceVo order = orderService.balanceOrder();
            model.addAttribute("balanceVo",order);
        } catch (ExecutionException e) {
            throw new RuntimeException(e);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        return "balance";
    }

    /**
     * 提交商品，完成支付后生成订单
     * @param vo
     * @return
     */
    @PostMapping("/orderSubmit")
    public String orderSubmit(OrderSubmitVo vo,Model model){
        R r = orderService.orderSubmit(vo);
        if (r.getCode() == 0){
            OrderEntity order = JSON.parseObject(JSON.toJSONString(r.get("order")), OrderEntity.class);
            model.addAttribute("order",order);
            return "cashier";
        }
        return "redirect:http://order.msb.com/toTrade";
    }

    /**
     * 平台在线支付
     * @param orderSN
     * @return
     */
    @PostMapping("/orderPay/returnUrl")
    public String orderPay(@RequestParam(value = "orderSN",required = false) String orderSN,
                           @RequestParam(value = "out_trade_no",required = false) String out_trade_no){
        if (!StringUtils.isNotBlank(orderSN)){
            orderService.updateOrderStatus(orderSN, OrderConstant.OrderStatusEnum.TO_SEND_GOODS.getCode());
        }else if (!StringUtils.isNotBlank(out_trade_no)){
            orderService.updateOrderStatus(out_trade_no, OrderConstant.OrderStatusEnum.TO_SEND_GOODS.getCode());
        }
        System.out.println("orderSN = " + orderSN);
        return "detail";
    }

    /**
     * 支付宝订单支付
     * @param orderSN
     * @return
     */
    @GetMapping(value = "/orderPay/zfb",produces = "text/html")
    @ResponseBody
    public String zfbOrderPay(@RequestParam("orderSN") String orderSN){
        PayVo payVo = orderService.getOrderByOrderSn(orderSN);
        String pay = payTemplate.pay(payVo);
        System.out.println("payVo = " + payVo);
        System.out.println("pay = " + pay);
        return pay;
    }

}
